package com.guli.gulimall.product.service.impl;

import com.guli.gulimall.product.dao.*;
import com.guli.gulimall.product.entity.SkuImagesEntity;
import com.guli.gulimall.product.entity.SpuInfoDescEntity;
import com.guli.gulimall.product.web.entity.vo.ItemSaleAttrVo;
import com.guli.gulimall.product.web.entity.vo.SkuItemVo;
import com.guli.gulimall.product.web.entity.vo.SpuItemAttrGroupVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.gulimall.product.entity.SkuInfoEntity;
import com.guli.gulimall.product.service.SkuInfoService;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {
    @Autowired
    private SkuInfoDao skuInfoDao;

    @Autowired
    private SkuImagesDao skuImagesDao;

    @Autowired
    private SkuSaleAttrValueDao skuSaleAttrValueDao;

    @Autowired
    private SpuInfoDescDao spuInfoDescDao;

    @Autowired
    private ProductAttrValueDao productAttrValueDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public SkuItemVo getItemBySkuId(Long skuId) {
        SkuItemVo skuItemVo = new SkuItemVo();

        //1、sku基本信息的获取  pms_sku_info
         SkuInfoEntity skuInfoEntity = skuInfoDao.selectById(skuId);
        skuItemVo.setSkuInfoEntity(skuInfoEntity);

        //2、sku的图片信息    pms_sku_images
         List<SkuImagesEntity> images = skuImagesDao.selectList(new QueryWrapper<SkuImagesEntity>().eq("sku_id",skuId));
         skuItemVo.setImages(images);

        //3、获取spu的销售属性组合
         List<ItemSaleAttrVo> saleAttr = skuSaleAttrValueDao.getItemSaleAttrVo(skuInfoEntity.getSpuId());
         skuItemVo.setSaleAttr(saleAttr);

        //4、获取spu的介绍
         SpuInfoDescEntity spuInfoDescEntity = spuInfoDescDao.selectById(skuInfoEntity.getSpuId());
        skuItemVo.setSpuInfoDescEntity(spuInfoDescEntity);

        //5、获取spu的规格参数信息
         List<SpuItemAttrGroupVo> groupAttrs = productAttrValueDao.getSpuItemAttrGroup(skuInfoEntity.getSpuId());
         skuItemVo.setGroupAttrs(groupAttrs);

        return skuItemVo;
    }

}